package com.yanqu.road.dao.impl.activity.drawPictrues;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.drawPictures.UserDrawPicturesActivityData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrawPicturesUserActivityDataDaoImpl extends TempDataObjectDao<UserDrawPicturesActivityData> {

    @Override
    protected boolean addData(UserDrawPicturesActivityData data) {
        String sql = "insert into t_u_activity_drawpictures(activityId,userId,currentPicturesId,integral,lastUpdateTime)" +
                "values(?,?,?,?,?)";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentPicturesId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserDrawPicturesActivityData data) {
        String sql = "update t_u_activity_drawpictures set currentPicturesId=?,integral=?,lastUpdateTime=? " +
                "where activityId=? and userId=?";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurrentPicturesId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserDrawPicturesActivityData parseData(ResultSet rs) throws SQLException {
        UserDrawPicturesActivityData userDrawPicturesActivityData = new UserDrawPicturesActivityData();
        userDrawPicturesActivityData.setActivityId(rs.getInt("activityId"));
        userDrawPicturesActivityData.setUserId(rs.getLong("userId"));
        userDrawPicturesActivityData.setCurrentPicturesId(rs.getInt("currentPicturesId"));
        userDrawPicturesActivityData.setIntegral(rs.getLong("integral"));
        userDrawPicturesActivityData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        return userDrawPicturesActivityData;
    }

    public Map<Integer, UserDrawPicturesActivityData> getDrawPicturesUserActivityData(List<Integer> activityIdList, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserDrawPicturesActivityData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_drawpictures where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDrawPicturesActivityData userDrawPicturesActivityData = parse(rs);
                dataMap.put(userDrawPicturesActivityData.getActivityId(), userDrawPicturesActivityData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
